package com.song.controller;

import com.alibaba.fastjson.JSON;
import com.song.dao.UserMapper;
import com.song.pojo.Orders;
import com.song.pojo.User;
import com.song.service.OrdersService;
import com.song.service.UserService;
import com.song.utils.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/27 - the current system date.
 * 16:36 - the current system time.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;

    @Autowired
    private OrdersService ordersService;

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public String register(User user){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用注册的方法
        if(userService.addUser(user)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"恭喜你,注册成功!");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"很遗憾,注册失败,请重新尝试!");
        }
        return JSON.toJSONString(map);
    }



    /**
     * 登录
     * @param
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(String loginName, String password, HttpSession Session){
        Map<String,Object> map = new HashMap<String,Object>();

        //调用注册的方法
        User loginUser = userService.login(loginName, password);

        //登录判断
        if(loginUser!=null){
            //将密码清空
            loginUser.setPassword(null);
            map.put(SystemConstant.SUCCESS,true);
            //保存登录用户信息到session中
            Session.setAttribute(SystemConstant.FRONT_LOGIN_USER,loginUser);
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"用户名或密码错误,请重新登录！");
        }
        return JSON.toJSONString(map);
    }





    /**
     * 根据用户名查询用户信息
     * @param loginName
     * @return
     */
    @RequestMapping("/checkName")
    @ResponseBody
    public String checkName(String loginName){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用注册的方法
        if(userService.findUserByName(loginName)!=null){
            map.put(SystemConstant.EXISI,true);
            map.put(SystemConstant.MESSAGE,"用户名存在，请重新输入");
        }else{
            map.put(SystemConstant.EXISI,false);
        }
        return JSON.toJSONString(map);
    }


    /**
     * 去到用户个人中心页面
     * @param session
     * @return
     */
    @RequestMapping("/toUser.html")
    public String PersonalCenter(HttpSession session, Model model){
        User user = (User) session.getAttribute(SystemConstant.FRONT_LOGIN_USER);
        //查询用户是否登录 若没登录跳转到登录页面
        if (user==null){
            //重定向到登录页面
            return "forward:/login.jsp";
        }
        Long id = user.getId();
        //通过用户id查询订单列表信息
        List<Orders> ordersList = ordersService.findByUserId(id);
        //将数据放入
        model.addAttribute("ordersList",ordersList);
        model.addAttribute("user",user);
        return "user";
    }


    /**
     * 完善个人用户中心
     * @param user
     * @return
     */
    @RequestMapping("/perfect")
    @ResponseBody
    public String perfect(User user){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用修改的方法
        if(userService.updateUserInfo(user)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"恭喜你,完善成功!");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"很遗憾,信息完善失败,请重新尝试!");
        }
        return JSON.toJSONString(map);
    }
}
